/******************************************************************************
 * Copyright (C) ShenZhen Powerdata Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳市博安达信息技术股份有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.szboanda.business.relevance.business.controller;

import com.github.pagehelper.PageInfo;
import com.szboanda.beijing.pure.BaseBusinessController;
import com.szboanda.business.relevance.business.exception.RelevanceBusinessException;
import com.szboanda.business.relevance.business.service.IRelevanceBusinessService;
import com.szboanda.platform.common.utils.MapUtils;
import com.szboanda.platform.common.utils.StringUtils;
import com.szboanda.platform.common.utils.Toolkit;
import com.szboanda.platform.rms.dept.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: 关联引擎关联关系组件_控制器
 * @fileName: RelevanceBusinessController.java
 * @description:
 * @copyright: PowerData Software Co.,Ltd. Rights Reserved.
 * @company: 深圳市博安达信息技术股份有限公司
 * @author: 原晋川
 * @date: 2019-09-21 18:35
 * @version: V1.0
 */

@Controller
@RequestMapping("/business/relevance/business/relevancebusinesscontroller")
public class RelevanceBusinessController extends BaseBusinessController {

    /**
     * 注入处理业务接口
     */
    @Autowired
    private IRelevanceBusinessService relevanceBusinessService;
    @Autowired
    private IDeptService deptService;


    /**
     * 跳转到【关联引擎关联关系组件】查询页面
     *
     * @return
     * @throws RelevanceBusinessException
     */
    @RequestMapping("/list")
    public String getListPageName() throws RelevanceBusinessException {
        this.setToken();
        return "/business/relevance/business/relevancebusinesslist";
    }

    /**
     * 跳转到【关联引擎关联关系组件】新增页面
     *
     * @return
     * @throws RelevanceBusinessException
     */
    @RequestMapping("/add")
    public String getAddPageName(ModelMap mp) throws RelevanceBusinessException {
        this.setToken();
        mp.put("XH_TEMP", Toolkit.getID());
        return "/business/relevance/business/relevancebusinessedit";
    }

    /**
     * 跳转到【关联引擎关联关系组件】编辑页面
     *
     * @param xh 业务数据序号
     * @param mp 页面View的Model对象
     * @return
     * @throws RelevanceBusinessException
     */
    @RequestMapping("/edit/{xh}")
    public String getEditPageName(@PathVariable String xh, ModelMap mp) throws RelevanceBusinessException {
        this.setToken();
        mp.put("XH", xh);
        return "/business/relevance/business/relevancebusinessedit";
    }

    /**
     * 跳转到【关联引擎关联关系组件】视图页面
     *
     * @param xh 业务数据序号
     * @param mp 页面View的Model对象
     * @return
     * @throws RelevanceBusinessException
     */
    @RequestMapping("/detail/{xh}")
    public String getDetailPageName(@PathVariable String xh, ModelMap mp) throws RelevanceBusinessException {
        this.setToken();
        mp.put("XH", xh);
        return "/business/relevance/business/relevancebusinessdetail";
    }

    /**
     * 功能描述: 加载
     *
     * @param
     * @return
     * @throws
     * @auther 原晋川
     * @date 2019/9/24 16:44
     */
    @RequestMapping(value = "/showList/{businessId}/{relevanceType}/{multiple}")
    public String showList(@PathVariable String businessId, @PathVariable String relevanceType, @PathVariable String multiple, ModelMap mp) throws RelevanceBusinessException {
        this.setToken();
        mp.put("businessId", businessId);
        mp.put("relevanceType", relevanceType);
        mp.put("multiple", multiple);
        return "/business/relevance/business/relevancebusinesslist";
    }

    @RequestMapping(value = "/taskList/{businessId}/{multiple}")
    public String taskList(@PathVariable String businessId, @PathVariable String multiple, ModelMap mp) throws RelevanceBusinessException {
        this.setToken();
        mp.put("businessId", businessId);
        mp.put("multiple", multiple);
        return "/business/relevance/business/taskList";
    }

    /**
     * 查询【关联引擎关联关系组件】单条业务信息
     *
     * @param xh 业务数据序号
     * @return 返回某个业务Map信息
     * @throws RelevanceBusinessException
     */
    @RequestMapping(value = "/detailData/{xh}")
    @ResponseBody
    public Map<String, Object> getDetailDataByXh(@PathVariable String xh) throws RelevanceBusinessException {
        return relevanceBusinessService.queryRelevanceBusinessByXh(xh);
    }

    /**
     * 查询【关联引擎关联关系组件】业务信息
     *
     * @param params 业务集合
     * @return 返回某个业务Map信息
     * @throws RelevanceBusinessException
     */
    @RequestMapping(value = "/queryRelevanceBusinesss")
    @ResponseBody
    public PageInfo<Map<String, Object>> queryRelevanceBusinesssInPage(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        int pageNum = getPageNum(params);//获取分页数
        int pageSize = getPageSize(params);//获取分页长度
        return relevanceBusinessService.queryRelevanceBusinesssInPage(pageNum, pageSize, params);
    }

    /**
     * 保存【关联引擎关联关系组件】业务信息
     *
     * @param params 业务Map集合
     * @return 返回操作标识符
     * @throws RelevanceBusinessException
     */
    @RequestMapping(value = "/saveRelevanceBusiness", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> saveRelevanceBusiness(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        if (!this.validToken(params)) {
            return null;
        }
        Map<String, Object> result = new HashMap<>();
        String businessId = MapUtils.getString(params, "businessId");
        List<Map<String,Map>> bussinessList = (List<Map<String,Map>>) params.get("businessList");

        //添加之前先删除
        relevanceBusinessService.deleteRelevanceBusinessByBusinessId(businessId);
        int count = 0;
        for (int i = 0; i < bussinessList.size(); i++) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("LYYWXH", businessId);
            paramMap.put("LYYWPZXH", businessId);
            paramMap.put("YWLX", bussinessList.get(i).get("YWLX"));
            paramMap.put("MBYWXH", bussinessList.get(i).get("YWXH"));
            paramMap.put("MBYWPZXH",  bussinessList.get(i).get("YWXH"));
            paramMap.put("YWBT",  bussinessList.get(i).get("YWBT"));
            paramMap.put("YWLXMC",  bussinessList.get(i).get("YWLXMC"));
            Map<String, Object> map = relevanceBusinessService.saveRelevanceBusiness(paramMap);
            count += MapUtils.getInt(map,"count");
        }

        //result.setToken(this.setToken());
        result.put("success","成功保存"+count+"条数据");
        return result;
    }

    @RequestMapping(value = "/saveTaskListRelevanceBusiness", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> saveTaskListRelevanceBusiness(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        if (!this.validToken(params)) {
            return null;
        }
        Map<String, Object> result = new HashMap<>();
        String businessId = MapUtils.getString(params, "businessId");
        List<Map<String,Map>> bussinessList = (List<Map<String,Map>>) params.get("businessList");

        //添加之前先删除
        relevanceBusinessService.deleteRelevanceBusinessByBusinessId(businessId);
        int count = 0;
        for (int i = 0; i < bussinessList.size(); i++) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("LYYWXH", businessId);
            paramMap.put("LYYWPZXH", businessId);
            paramMap.put("YWLX", "RW");
            paramMap.put("MBYWXH", bussinessList.get(i).get("YWXH"));
            paramMap.put("MBYWPZXH",  bussinessList.get(i).get("YWXH"));
            paramMap.put("YWBT",  bussinessList.get(i).get("NR"));
            paramMap.put("YWLXMC",  "任务");
            Map<String, Object> map = relevanceBusinessService.saveRelevanceBusiness(paramMap);
            count += MapUtils.getInt(map,"count");
        }

        //result.setToken(this.setToken());
        result.put("success","成功保存"+count+"条数据");
        return result;
    }

    /**
     * 删除【关联引擎关联关系组件】业务信息
     *
     * @param params 业务集合
     * @return 返回操作标识符
     * @throws RelevanceBusinessException
     */
    @RequestMapping(value = "/deleteRelevanceBusiness", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> deleteRelevanceBusiness(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        if (!this.validToken(params)) {
            return null;
        }
        Map<String, Object> result = new HashMap<>();
        int count = relevanceBusinessService.deleteRelevanceBusiness(params);
        //result.setToken(this.setToken());
        if(count>0){
            result.put("success","成功解除"+count+"条数据");
        }else{
            result.put("success","解除失败");
        }
        return result;
    }

    /**
     * 功能描述: 根据类型SQL查询数据
     *
     * @param params
     * @return datalist
     * @throws RelevanceBusinessException
     * @auther 原晋川
     * @date 2019/9/23 14:40
     */
    @RequestMapping(value = "/queryDataByType", method = {RequestMethod.POST})
    @ResponseBody
    public PageInfo<Map<String, Object>> queryDataByType(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        int pageNum = getPageNum(params);//获取分页数
        int pageSize = getPageSize(params);//获取分页长度
        if (!this.validToken(params)) {
            return null;
        }
        PageInfo<Map<String, Object>> pageInfo = null;

        List<String> bmlist = getBmxhs(this.getCurrUser().getBmbh());
        if(bmlist.size()==0){

        }else{
            params.put("BMLIST",getBmxhs(this.getCurrUser().getBmbh()));
            pageInfo =  relevanceBusinessService.queryListByConfig(pageNum,pageSize,params);
        }

         return pageInfo;
    }

    @RequestMapping(value = "/taskListData", method = {RequestMethod.POST})
    @ResponseBody
    public PageInfo<Map<String, Object>> taskListData(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        int pageNum = getPageNum(params);//获取分页数
        int pageSize = getPageSize(params);//获取分页长度
        if (!this.validToken(params)) {
            return null;
        }
        PageInfo<Map<String, Object>> pageInfo = null;
        pageInfo =  relevanceBusinessService.queryTaskListByConfig(pageNum,pageSize,params);
        return pageInfo;
    }

    private List<String> getBmxhs(String bmbh){
        List<String> bmList = new ArrayList<>();
        Map<String,Object> bm = deptService.getDeptByXh(bmbh);
        String XH = MapUtils.getString(bm,"XH");
        String SJZZXH = MapUtils.getString(bm,"SJZZXH");
        if("ROOT".equals(SJZZXH)){
            List<Map<String,Object>> bms = deptService.getLowerDeptByXh(XH);
            if(bms!=null && bms.size()>0){
                bms.stream().forEach(o->{
                    bmList.add(MapUtils.getString(o,"ZZBH"));
                });
                bmList.add(XH);
            }
        }else{
            List<Map<String,Object>> bms = deptService.getLowerDeptByXh(SJZZXH);
            if(bms!=null && bms.size()>0){
                bms.stream().forEach(o->{
                    bmList.add(MapUtils.getString(o,"ZZBH"));
                });
                if(StringUtils.isNotEmpty(SJZZXH)){
                    bmList.add(SJZZXH);
                }

            }
        }
        return bmList;
    }


    /**
     * 功能描述: 根据业务关联序号查询已关联业务
     *
     * @param
     * @return
     * @throws
     * @auther 原晋川
     * @date 2019/9/25 14:33
     */
    @ResponseBody
    @RequestMapping(value = "/queryDataByBusiness", method = {RequestMethod.POST})
    public List<Map<String, Object>> queryDataByBusiness(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        //获取分页长度
        if (!this.validToken(params)) {
            return null;
        }
        List<Map<String, Object>> mapList = relevanceBusinessService.queryRelevanceBusinesss(params);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "/queryTaskListRelevanceBusinesss", method = {RequestMethod.POST})
    public List<Map<String, Object>> queryTaskListRelevanceBusinesss(@RequestBody Map<String, Object> params) throws RelevanceBusinessException {
        //获取分页长度
        if (!this.validToken(params)) {
            return null;
        }
        List<Map<String, Object>> mapList = relevanceBusinessService.queryTaskListRelevanceBusinesss(params);
        return mapList;
    }

}
